<?php

/**
 * +----------------------------------------------------------------------
 * | TickyPHP [ This is a freeware ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2015 All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
 * +----------------------------------------------------------------------
 * | SVN: $Id: Cron.php 71410 2018-10-30 09:12:08 luomingui $
 * +----------------------------------------------------------------------
 * | 文件功能：对应的表名:tky_cron
 * +----------------------------------------------------------------------
 */

namespace application\admin\controller;

use ticky\request;
use ticky\response;

class cron extends base {

    //首页
    public function index() {
        $search = $this->search_frm();
        $ret = db('cron')->where($search['sql'])->newpage();
        $this->assign('page', $ret['page']);
        $this->assign('items', $ret['items']);
        $this->assign('search', $search['arr']);
        $this->display('cron/index');
    }

    // 操作管理
    public function manage() {
        $cronid = request::get('id', 0);
        if ($cronid <= 0) {
            $this->assign('postUrl', '/admin/cron/add');
            $this->assign('action', '添加');
        } else {
            $item = db('cron')->where(['cronid' => $cronid])->find();
            $this->assign('postUrl', '/admin/cron/update');
            $this->assign('action', '修改');
            $this->assign('item', $item);
        }
        $this->display('cron/manage');
    }

    // 添加记录
    public function add() {
        $data = $this->post_frm();
        db('cron')->insert($data);
        showmsg('添加成功', '/admin/cron');
    }

    // 更新记录
    public function update() {
        $data = $this->post_frm();
        $id = request::post('cronid', 0);
        db('cron')->where(['cronid' => $id])->update($data);
        showmsg('修改成功', '/admin/cron');
    }

    // 删除记录
    public function delete() {
        $id = request::get('id', 0);
        $result = db('cron')->delete(['cronid' => $id]);
        showmsg('删除成功', '/admin/cron');
    }

    // 批量删除
    public function batchremove() {
        $optype = request::post('optype', 'del');
        $ids = request::post('ids', []);
        if ($optype == "del") {
            for ($i = 0; $i < count($ids); $i++) {
                $id = $ids[$i];
                db('cron')->delete(['cronid' => $id]);
            }
            showmsg('删除成功', '/admin/cron');
        }
    }

    //查询条件
    private function search_frm() {
        $search = request::get('search', []);
        $where = '1=1 ';

        if ($cronid = trim($search['cronid'])) {
            $where .= "and cronid = '{$cronid}' ";
        }

        if ($available = trim($search['available'])) {
            $where .= "and available = '{$available}' ";
        }

        if ($type = trim($search['type'])) {
            $where .= "and type = '{$type}' ";
        }

        if ($name = trim($search['name'])) {
            $where .= "and name = '{$name}' ";
        }

        if ($filename = trim($search['filename'])) {
            $where .= "and filename = '{$filename}' ";
        }

        if ($lastrun = trim($search['lastrun'])) {
            $where .= "and lastrun = '{$lastrun}' ";
        }

        if ($nextrun = trim($search['nextrun'])) {
            $where .= "and nextrun = '{$nextrun}' ";
        }

        if ($weekday = trim($search['weekday'])) {
            $where .= "and weekday = '{$weekday}' ";
        }

        if ($day = trim($search['day'])) {
            $where .= "and day = '{$day}' ";
        }

        if ($hour = trim($search['hour'])) {
            $where .= "and hour = '{$hour}' ";
        }

        if ($minute = trim($search['minute'])) {
            $where .= "and minute = '{$minute}' ";
        }

        return [
            'arr' => $search,
            'sql' => $where
        ];
    }

    //表单数据
    private function post_frm() {
        $arr = array();
        $arr['available'] = request::post('available', '');
        $arr['type'] = request::post('type', '');
        $arr['name'] = request::post('name', '');
        $arr['filename'] = request::post('filename', '');
        $arr['lastrun'] = request::post('lastrun', '');
        $arr['nextrun'] = request::post('nextrun', '');
        $arr['weekday'] = request::post('weekday', '');
        $arr['day'] = request::post('day', '');
        $arr['hour'] = request::post('hour', '');
        $arr['minute'] = request::post('minute', '');
        return $arr;
    }

}
